package com.empire.module.member.service.mappolygonlog;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.empire.module.member.controller.admin.mappolygonlog.vo.*;
import com.empire.module.member.dal.dataobject.mappolygonlog.MapPolygonLogDO;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.util.object.BeanUtils;

import com.empire.module.member.dal.mysql.mappolygonlog.MapPolygonLogMapper;

import javax.annotation.Resource;

import static com.empire.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.empire.module.member.enums.ErrorCodeConstants.MAP_POLYGON_LOG_NOT_EXISTS;

/**
 * 多边形操作日志 Service 实现类
 *
 * @author 主席大人
 */
@Service
@Validated
public class MapPolygonLogServiceImpl implements MapPolygonLogService {

    @Resource
    private MapPolygonLogMapper mapPolygonLogMapper;

    @Override
    public Long createMapPolygonLog(MapPolygonLogSaveReqVO createReqVO) {
        // 插入
        MapPolygonLogDO mapPolygonLog = BeanUtils.toBean(createReqVO, MapPolygonLogDO.class);
        mapPolygonLogMapper.insert(mapPolygonLog);
        // 返回
        return mapPolygonLog.getId();
    }

    @Override
    public void updateMapPolygonLog(MapPolygonLogSaveReqVO updateReqVO) {
        // 校验存在
        validateMapPolygonLogExists(updateReqVO.getId());
        // 更新
        MapPolygonLogDO updateObj = BeanUtils.toBean(updateReqVO, MapPolygonLogDO.class);
        mapPolygonLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteMapPolygonLog(Long id) {
        // 校验存在
        validateMapPolygonLogExists(id);
        // 删除
        mapPolygonLogMapper.deleteById(id);
    }

    private void validateMapPolygonLogExists(Long id) {
        if (mapPolygonLogMapper.selectById(id) == null) {
            throw exception(MAP_POLYGON_LOG_NOT_EXISTS);
        }
    }

    @Override
    public MapPolygonLogDO getMapPolygonLog(Long id) {
        return mapPolygonLogMapper.selectById(id);
    }

    @Override
    public PageResult<MapPolygonLogDO> getMapPolygonLogPage(MapPolygonLogPageReqVO pageReqVO) {
        return mapPolygonLogMapper.selectPage(pageReqVO);
    }

}